﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.Model;
using FantastPlatform.Framework.Service;
using FantastPlatform.Community.IService;
using System.Data.SqlClient;
using System.Configuration;
using System.Threading;
using NHibernate;
using NHibernate.Criterion;

namespace FantastPlatform.Community.Service
{
    public class PersonService : DataService<IPerson, Person>, IPersonService
    {
        /// <summary>
        /// 人员信息查询(通过内网)
        /// </summary>
        /// <param name="idCard">身份证号码</param>
        /// <param name="phoneNumber">查询人手机号码</param>
        /// <param name="requestType">查询类型</param>
        /// <returns></returns>
        public IPerson SearchPersonByIdCard(string idCard, string phoneNumber)
        {
            string content = "";
            IPerson person = null;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GpsPatrol"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(" INSERT INTO [PersonRequestInfo]  ([IdCard],[PhoneNumber],[State],[RequestTime],[CheckType])VALUES('" + idCard + "','" + phoneNumber + "',0,getdate(),'" + 1 + "')", conn))
                {
                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            int i = 10;
            i = Convert.ToInt32(ConfigurationManager.AppSettings["SearchCar_Sleep"].ToString());
            int count = 3;
            count = Convert.ToInt32(ConfigurationManager.AppSettings["SearchCar_SleepTimes"].ToString());

            for (int j = 0; j < count; j++)
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GpsPatrol"].ConnectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("select Id, Content From [PersonReply] WHERE IdCard = '" + idCard + "'And PhoneNumber = '" + phoneNumber + "' And State = 0 And CheckType = '" + 1 + "'", conn))
                    {
                        SqlDataReader reader = cmd.ExecuteReader();
                        int id = 0;
                        if (reader.Read())
                        {
                            id = reader.GetInt32(0);
                            content = reader.GetString(1);
                            reader.Close();
                            cmd.CommandText = "Update [PersonReply] set state=1 where Id='" + id + "'";
                            cmd.ExecuteNonQuery();
                            if (!string.IsNullOrEmpty(content))
                            {
                                content = "{" + content + "}";
                                List<string> propertyList = content.Split(',').ToList();
                                foreach (string item in propertyList)
                                {
                                    string[] propertyArry = item.Split(':');
                                    if (propertyArry.Length > 1)
                                    {
                                        switch (propertyArry[0])
                                        {
                                            case "\"身份证号\"":
                                                person.IdCard = propertyArry[1].Substring(1, propertyArry[1].Length - 1);
                                                break;
                                            case "\"姓名\"":
                                                person.Name = propertyArry[1].Substring(1, propertyArry[1].Length - 1);
                                                break;
                                            case "\"性别\"":
                                                person.Sex = propertyArry[1].Substring(1, propertyArry[1].Length - 2);
                                                break;
                                            case "\"民族\"":
                                                person.Nation = propertyArry[1].Substring(1, propertyArry[1].Length - 1);
                                                break;
                                            case "\"出生地\"":
                                                person.RegisterAddress = propertyArry[1].Substring(1, propertyArry[1].Length - 1);
                                                break;
                                            case "\"出生地XZ\"":
                                                person.RegisterAddress = propertyArry[1].Substring(1, propertyArry[1].Length - 1);
                                                break;
                                            case "\"籍贯省市县区\"":
                                                person.NativePlace = propertyArry[1].Substring(1, propertyArry[1].Length - 1);
                                                break;
                                            case "\"住址详细\"":
                                                person.Address = propertyArry[1].Substring(1, propertyArry[1].Length - 1);
                                                break;
                                        }
                                    }
                                }
                            }
                            break;
                        }
                    }
                    conn.Close();
                }
                Thread.Sleep(10000);
            }
            return person;
        }


        public IList<IPerson> GetPersonByIdCard(string idCard)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(Person));
            criteria.Add(Expression.Eq("IdCard", idCard)).Add(Expression.Eq("Enable", true));
            return criteria.List<IPerson>();
        }
    }
}
